layui.use(['table', 'layer', 'treetable', 'jquery'], function () {
    var table = layui.table,
        layer = layui.layer,
        treeTable = layui.treetable;
        $ = layui.jquery;

    treeTable.render({
        treeColIndex: 1,
        treeSpid: 1,
        treeIdName: 'id',
        treePidName: 'pId',
        elem: '#munu-table',
        url: ctx + "/permission/module",
        toolbar: "#toolbarDemo",
        treeDefaultClose: true,
        page: true,
        cols: [[
            {type: "numbers"},
            {
                field: "title", minWidth: 100, title: "菜单名称"
            },
            {
                field: "resourceType", title: "角色名称", width: 100
            },
            {
                field: "href", title: "菜单路径"
            },
            {field: 'createTime', title: '创建时间'},
            {field: 'updatetime', title: '更新时间'},
            {templet: '#auth-state', width: 320, align: 'center', title: '操作'}
        ]]
    });
    table.on('toolbar(munu-table)', function (data) {


        // 判断layevent
        if (data.event == 'expand') {
            // 全部展开
            treeTable.expandAll("#munu-table")
        } else if (data.event == 'fold') {
            //全部折叠
            treeTable.foldAll("#munu-table")
        } else if (data.event == 'add') {
            //添加目录 层级为0  父级菜单1
            openAddModuleDialog(0, 1);
        }
    })
    //监听行工具栏
    table.on('tool(munu-table)', function (data) {




        if (data.event == 'add') {
            // 添加子项
            //判断当前的层级 如果是三级菜单 则不能添加
            if (data.data.isType == 2) {
                layer.msg("暂不支持添加三级菜单", {icon: 5})
                return;
            } else {
                //一级菜单 或者是二级菜单   grade  当前层级 +1  parentId = 当前资源ID
                openAddModuleDialog(data.data.isType + 1, data.data.id);
            }
        } else if (data.event == 'edit') {// 修改资源
            openUpdateModuleDialog(data.data.id);
        } else if (data.event == 'del') {
            // 执行删除资源操作
            deleteModule(data.data.id);
        }
    })

    function openAddModuleDialog(isType, pId) {
        <!-- 头部按钮添加资源-->
        var title = "<h3>菜单添加 -- 添加资源 </h3>"
        var url = ctx + "/permission/toAddModulePage?isType=" + isType + "&&pId=" + pId;
        layui.layer.open({
            title: title,
            type: 2,
            area: ["700px", "450px"],
            maxmin: true,
            content: url,
            shadeClose: true
        })
    }
    function openUpdateModuleDialog(id) {
       // 更新资源
        var title = "<h3>菜单添加 -- 更新资源 </h3>"
        var url = ctx + "/permission/toUpdateModulePage?id=" + id;
        layui.layer.open({
            title: title,
            type: 2,
            area: ["700px", "450px"],
            maxmin: true,
            content: url,
            shadeClose: true
        })
    }

    function deleteModule(id) {
        layer.confirm("确认删除记录吗?", {icon: 3, title: "资源管理"}, function (data) {
            // 确认删除发送ajax 请求
            $.post(ctx + "/permission/delete", {id: id}, function (result) {
                // 判断是否成功
                if (result.code == 200) {
                    layer.msg("删除成功" ,{icon:6});
                    window.location.reload() ;
                } else {
                    layer.msg(result.msg, {icon: 5})
                }
            })
        })
    }


})